package com.imonsoft.sqm.service.excel.IncomingExcel;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imonsoft.common.util.StringUtil;
import com.imonsoft.common.util.ValidatorUtil;
import com.imonsoft.sqm.pojo.basedata.Part;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.service.basedata.PartService;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.web.excel.IncomingExcel.IncomingImportDto;
@Service("incomingExcelService")
public class IncomingExcelServiceImpl implements IncomingExcelService {
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private PartService partService;

	@Override
	public List<String> txCheckData(IncomingImportDto incomingImportDto) {
		List<String> messages = new ArrayList<String>();
		List<Supplier> supplierList = supplierService.queryAll();
		List<Part> partList = partService.queryAll();
		if (supplierList == null || supplierList.size() <= 0) {
			messages.add("系统中无供应商数据，请先维护供应商数据!");
		}
		if (partList == null || partList.size() <= 0) {
			messages.add("系统中无零件数据，请先维护零件数据!");
		}
		if (StringUtils.isEmpty(incomingImportDto.getSupplierCode())) {
			messages.add("第[" + incomingImportDto.getRow() + "]行供应商编号不能为空!");
		} else {
			if (supplierList != null && supplierList.size() > 0) {
				for (Supplier s : supplierList) {
					if (incomingImportDto.getSupplierCode().equals(s.getCode())) {
						incomingImportDto.setSupplierId(Long.parseLong(StringUtil.fixup(s.getId())));
						incomingImportDto.setSupplierName(s.getName());
					}
				}
			}
			if (incomingImportDto.getSupplierId() == null) {
				messages.add("第[" + incomingImportDto.getRow() + "]行供应商编号不存在!");
			}
		}

		if (StringUtils.isEmpty(incomingImportDto.getPartCode())) {
			messages.add("第[" + incomingImportDto.getRow() + "]行零件编号不能为空!");
		} else {
			if (partList != null && partList.size() > 0) {
				for (Part s : partList) {
					if (incomingImportDto.getPartCode().equals(s.getCode())) {
						incomingImportDto.setPartId(Long.parseLong(StringUtil.fixup(s.getId())));
						incomingImportDto.setPartName(s.getName());
					}
				}
			}
			if (incomingImportDto.getPartId() == null) {
				messages.add("第[" + incomingImportDto.getRow() + "]行零件编号不存在!");
			}
		}
		if (StringUtils.isEmpty(incomingImportDto.getYearStr())) {
			messages.add("第[" + incomingImportDto.getRow() + "]行年份不能为空!");
		} else if (!ValidatorUtil.isNumber(incomingImportDto.getYearStr())) {
			messages.add("第[" + incomingImportDto.getRow() + "]行请输入正确的年份!");
		} else if (StringUtil.toLong(incomingImportDto.getYearStr()) <= 0) {
			messages.add("第[" + incomingImportDto.getRow() + "]行请输入正确的年份!");
		}
		if (StringUtil.isNotEmpty(incomingImportDto.getCountJanuary())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountJanuary())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行1月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountJanuary()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行1月零件数量请输入大于0的整数数值!");
			}
		}
		if (StringUtil.isNotEmpty(incomingImportDto.getCountFebruary())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountFebruary())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行2月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountFebruary()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行2月零件数量请输入大于0的整数数值!");
			}
		}

		if (StringUtil.isNotEmpty(incomingImportDto.getCountMarch())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountMarch())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行3月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountMarch()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行3月零件数量请输入大于0的整数数值!");
			}
		}

		if (StringUtil.isNotEmpty(incomingImportDto.getCountApril())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountApril())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行4月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountApril()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行4月零件数量请输入大于0的整数数值!");
			}
		}

		if (StringUtil.isNotEmpty(incomingImportDto.getCountMay())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountMay())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行5月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountMay()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行5月零件数量请输入大于0的整数数值!");
			}
		}

		if (StringUtil.isNotEmpty(incomingImportDto.getCountJune())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountJune())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行6月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountJune()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行6月零件数量请输入大于0的整数数值!");
			}
		}

		if (StringUtil.isNotEmpty(incomingImportDto.getCountJuly())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountJuly())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行7月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountJuly()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行7月零件数量请输入大于0的整数数值!");
			}
		}

		if (StringUtil.isNotEmpty(incomingImportDto.getCountAugust())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountAugust())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行8月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountAugust()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行8月零件数量请输入大于0的整数数值!");
			}
		}

		if (StringUtil.isNotEmpty(incomingImportDto.getCountSeptember())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountSeptember())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行9月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountSeptember()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行9月零件数量请输入大于0的整数数值!");
			}
		}

		if (StringUtil.isNotEmpty(incomingImportDto.getCountOctober())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountOctober())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行10月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountOctober()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行10月零件数量请输入大于0的整数数值!");
			}
		}

		if (StringUtil.isNotEmpty(incomingImportDto.getCountNovember())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountNovember())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行11月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountNovember()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行11月零件数量请输入大于0的整数数值!");
			}
		}

		if (StringUtil.isNotEmpty(incomingImportDto.getCountDecember())) {
			if (!ValidatorUtil.isNumber(incomingImportDto.getCountDecember())) {
				messages.add("第[" + incomingImportDto.getRow() + "]行12月零件数量请输入数值!");
			} else if (StringUtil.toLong(incomingImportDto.getCountDecember()) <= 0) {
				messages.add("第[" + incomingImportDto.getRow() + "]行12月零件数量请输入大于0的整数数值!");
			}
		}
		return messages;
	}
}
